/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */
// 移除每个右侧有一个更大数值的节点。

// 尝试从尾部回溯

function ListNode(val, next) {
        this.val = (val===undefined ? 0 : val)
        this.next = (next===undefined ? null : next)
}
function removeNodes(head){
     if (head.next) {
          const next = removeNodes(head.next)
          if (head.val < next.val) {
               return next
          }
          // 疏漏的点，即便未能满足条件，也应把 next 拼接过来。 否则直接返回head，会导致之前的比较 结果 失效
          head.next = next
          return head
     } else {
          return head
     }
};


const arr = [5,2,13,3,8]
const item5 = { val: 8 }
const item4 = { val: 3, next: item5 }
const item3 = { val: 14, next: item4 }
const item2 = { val: 7, next: item3 }
const item1 = { val: 5, next: item2 }